package com.woniu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.dto.CheckPriceDto;
import com.woniu.dto.CheckPriceListDto;
import com.woniu.dto.PageDto;
import com.woniu.exception.CheckException;
import com.woniu.model.HisCheckPrice;
import com.woniu.mapper.HisCheckPriceMapper;
import com.woniu.param.CreateOrModifyCheckPriceParam;
import com.woniu.param.DeleteCheckPriceParam;
import com.woniu.param.PageParam;
import com.woniu.param.QueryCheckPriceParam;
import com.woniu.service.IHisCheckPriceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 检查价格表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-11-11
 */
@Service
public class HisCheckPriceServiceImpl extends ServiceImpl<HisCheckPriceMapper, HisCheckPrice> implements IHisCheckPriceService {

    @Override
    public CheckPriceDto createCheckPrice(CreateOrModifyCheckPriceParam createOrmodifyCheckPriceParam) {
        HisCheckPrice checkPrice = new HisCheckPrice();
        BeanUtils.copyProperties(createOrmodifyCheckPriceParam, checkPrice);

        if (!this.saveOrUpdate(checkPrice)) {
            throw new CheckException(1035, "项目检查价格保存或更新失败");
        }
        CheckPriceDto dto = new CheckPriceDto();
        BeanUtils.copyProperties(checkPrice, dto);
        return dto;
    }

    @Override
    public CheckPriceListDto queryCheckPrice(QueryCheckPriceParam queryCheckPriceParam) {

        QueryWrapper<HisCheckPrice> wrapper = new QueryWrapper<>();

        //查询条件
        if(ObjectUtil.isNotNull(queryCheckPriceParam.getQueryCriteriaParam())) {
            if(ObjectUtil.isNotNull(queryCheckPriceParam.getQueryCriteriaParam().getCheckPriceId())) {
                wrapper.eq("check_price_id", queryCheckPriceParam.getQueryCriteriaParam().getCheckPriceId());
            }
            if(ObjectUtil.isNotNull(queryCheckPriceParam.getQueryCriteriaParam().getCheckPriceName())) {
                wrapper.eq("check_price_name", queryCheckPriceParam.getQueryCriteriaParam().getCheckPriceName());
            }
        }

        CheckPriceListDto dto = new CheckPriceListDto();
        //不需要分页
        if (BeanUtil.isEmpty(queryCheckPriceParam.getPageParam())) {
            queryCheckPriceParam.setPageParam(new PageParam(1L, 1000L));
        }
        Page<HisCheckPrice> page = this.page(new Page<>(queryCheckPriceParam.getPageParam().getPage(),
                queryCheckPriceParam.getPageParam().getPageSize()), wrapper);
//            BeanUtils.copyProperties(page, dto.getPageDto());

        dto.setPageDto(PageDto.bulid(page, CheckPriceDto.class));
        return dto;
    }

    @Override
    public void deleteCheckPrice(DeleteCheckPriceParam deleteCheckPriceParam) {

        if (ObjectUtil.isNull(deleteCheckPriceParam.getCheckPriceIdList()) || deleteCheckPriceParam.getCheckPriceIdList().size()==0) {
            throw new CheckException(1007, "删除项目无参数");
        }

        if (!this.removeByIds(deleteCheckPriceParam.getCheckPriceIdList())) {
            throw new CheckException(1008, "删除项目失败");
        }

    }
}
